package com.hkbigdata.sybx.controller;

import com.hkbigdata.sybx.bean.UserProfile;
import com.hkbigdata.sybx.mapper.UserProfileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangjing
 * @creat 2025-06-04-10:25
 * @email 3010335359@qq.com
 */

@RestController
@RequestMapping("/api/userProfile")
public class UserProfileController {

    @Autowired
    private UserProfileMapper profileMapper;

    /**
     * 添加用户资料（增加存在性检查）
     */
    @PostMapping("/insert")
    public Map<String, Object> addUserProfile(@RequestBody UserProfile profile) {
        Map<String, Object> result = new HashMap<>();

        // 检查资料是否已存在
        UserProfile existsProfile = profileMapper.getUserProfileByUserId(profile.getUserId());
        if (existsProfile != null) {
            result.put("success", false);
            result.put("message", "资料已存在，不能重复添加");
            return result;
        }

        try {
            int rows = profileMapper.addUserProfile(profile);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "添加成功");
                result.put("data", profile);
            } else {
                result.put("success", false);
                result.put("message", "添加失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加异常：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 更新用户资料
     */
    @PutMapping("/update")
    public Map<String, Object> updateUserProfile(@RequestBody UserProfile profile) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = profileMapper.updateUserProfile(profile);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "更新成功");
                result.put("data", profile);
            } else {
                result.put("success", false);
                result.put("message", "记录不存在或更新失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新异常：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 通过userId查询用户资料
     */
    @GetMapping("/{userId}")
    public Map<String, Object> getUserProfile(@PathVariable String userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            UserProfile profile = profileMapper.getUserProfileByUserId(userId);
            if (profile != null) {
                result.put("success", true);
                result.put("message", "查询成功");
                result.put("data", profile);
            } else {
                result.put("success", false);
                result.put("message", "记录不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
    /*
    * 查看全部用户资料
    * */
    @GetMapping("/all")
    public Map<String, Object> getAllUserProfile() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<UserProfile> profiles = profileMapper.getAllProfiles();
            if (profiles != null && !profiles.isEmpty()) {
                result.put("success", true);
                result.put("message", "查询成功");
                result.put("data", profiles);
            } else {
                result.put("success", false);
                result.put("message", "记录不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}